// pages/cart/cart.js
import { getcart, getremove, getemodifyNumber, getselect } from "../../utils/http"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodds: [],
    price: [],
    number: 0,
    num:[],
    // selecteds:false,
    // num: 0,
  },
  // 数量++
  addjj(e) {
    console.log(e);
    let { num } = this.data;
    let key = e.currentTarget.dataset.key
    let number = e.currentTarget.dataset.number
    console.log(key);
    // 数量++
    getemodifyNumber({ key: e.currentTarget.dataset.key, number: number + 1, token: wx.getStorageSync("token") }).then(res => {
      console.log(res);
      this.setData({
        number: res.data.items.number
      })
      console.log(number);
      getcart({ token: wx.getStorageSync('token') }).then(res => {
        console.log(res);
        if (res.code === 700) {
          this.setData({
            goodds: []
          })
        } else {
          this.setData({
            goodds: res.data.items || [],
            price: res.data.price,
            number: res.data.number
          })
          this.setbadge(res.data.number)
        }

        // console.log(goodds)
      })
    })
  },
  // 状态
  checkeds(e) {
    let { selecteds } = this.data
    let key = e.currentTarget.dataset.key
    let selected = e.currentTarget.dataset.selected
    console.log(e);
    getselect({ key: key, selected: !selected, token: wx.getStorageSync("token") }).then(res => {
      console.log(res);
      this.setData({
        selected: res.data.items.selected
      })
      getcart({ token: wx.getStorageSync('token') }).then(res => {
        console.log(res);
        if (res.code === 700) {
          this.setData({
            goodds: []
          })
        } else {
          this.setData({
            goodds: res.data.items || [],
            price: res.data.price,
            number: res.data.number
          })
          this.setbadge(res.data.number)
        }

        // console.log(goodds)
      })
    })
  },
  // 删除 数量--
  remove(e) {
    console.log(e);
    let { num } = this.data;
    let key = e.currentTarget.dataset.key
    let number = e.currentTarget.dataset.number
    console.log(key);
    // 数量--
    getemodifyNumber({ key: e.currentTarget.dataset.key, number: number-1, token: wx.getStorageSync("token") }).then(res => {
      console.log(res);
      this.setData({
        number: res.data.items.number
      })
      console.log(num);
      if(number=1){
        getremove({ key: key, token: wx.getStorageSync("token") }).then(res => {
          console.log(res);
        })
      }
      // 重新渲染
      getcart({ token: wx.getStorageSync('token') }).then(res => {
        console.log(res);
        if (res.code === 700) {
          this.setData({
            goodds: []
          })
        } else {
          this.setData({
            goodds: res.data.items || [],
            price: res.data.price,
            number: res.data.number
          })
          this.setbadge(res.data.number)
        }

        // console.log(goodds)
      })
      
    })
    //  删除
   
  },
  // 图标
  setbadge(num) {
    wx: wx.setTabBarBadge({
      index: 2,
      text: String(num),
      //  success: (res) => {},
      //  fail: (res) => {},
      //  complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let { goodds, price, number } = this.data
    getcart({ token: wx.getStorageSync('token') }).then(res => {
      console.log(res);
      if (res.code === 700) {
        this.setData({
          goodds: []
        })
      } else {
        this.setData({
          goodds: res.data.items || [],
          price: res.data.price,
          number: res.data.number
        })
        this.setbadge(res.data.number)
      }

      // console.log(goodds)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})